package com.kanche.bigbaby.collection;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.dialog.DatePickerFragment;
import com.kanche.bigbaby.collection.dialog.VehicleDescriptionEditDialog;
import com.kanche.bigbaby.collection.dialog.VinRecognitionDialog;
import com.kanche.bigbaby.collection.domain.Vehicle;
import com.kanche.bigbaby.collection.domain.VehicleDescHighlight;
import com.kanche.bigbaby.collection.domain.VehicleDescIndividual;
import com.kanche.bigbaby.collection.ui.ItemConfig;
import com.kanche.bigbaby.collection.ui.ItemDatePickerConfig;
import com.kanche.bigbaby.collection.ui.ItemEditTextWithActionButtonConfig;
import com.kanche.bigbaby.collection.ui.ItemSelectorConfig;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.kkche.merchant.adpaters.ItemsAdapter;
//import com.kkche.merchant.awesome.DrawableAwesome;
//import com.kkche.merchant.dialogs.DatePickerFragment;
//import com.kkche.merchant.dialogs.VehicleDescriptionEditDialog;
//import com.kkche.merchant.dialogs.VinRecognitionDialog;
//import com.kkche.merchant.domain.Vehicle;
//import com.kkche.merchant.domain.VehicleDescHighlight;
//import com.kkche.merchant.domain.VehicleDescIndividual;
//import com.kkche.merchant.domain.ui.ItemConfig;
//import com.kkche.merchant.domain.ui.ItemDatePickerConfig;
//import com.kkche.merchant.domain.ui.ItemEditTextWithActionButtonConfig;
//import com.kkche.merchant.domain.ui.ItemSelectorConfig;

/**
 * Created by nealmi on 6/10/14.
 */
public class VehicleEditorStep2Activity extends BaseActivity implements AdapterView.OnItemClickListener,
        DatePickerFragment.OnDateSelectedListener, VehicleDescriptionEditDialog.onDoneListener,
        ItemEditTextWithActionButtonConfig.OnActionListener {
    public static final int REQUEST_CODE_VEHICLE_MODEL = 200;
    private static final int REQUEST_CODE_SCAN_VIN_BARCODE = 300;
    private static final int STEP3 = 201;
    private static final int REQUEST_CODE_DESCRIPTION = 301;
    private ListView listView;
    private ItemsAdapter adapter;
    private String itemKey;
    private DatePickerFragment datePickerFragment;
    private Vehicle vehicle;

    private long rowId;
    private KKCheDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vehicle_editor_step2);

        toolbar = justFindIt(R.id.toolbar);
        toolbar.setTitle("必填信息");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        dbHelper = new KKCheDBHelper(this);
        listView = (ListView) findViewById(R.id.listView);

        rowId = getIntent().getLongExtra("rowId", 0L);
        vehicle = dbHelper.findVehicleByRowId(rowId);

        if (vehicle == null) {
            vehicle = Vehicle.NULL;
        }
//        vehicle.setVin(UUID.randomUUID().toString().substring(0, 16));
        List<ItemConfig> items = new ArrayList<ItemConfig>();
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.vin)
                .label(getString(R.string.vin_label))
                .hint(getString(R.string.vin_input_hint))
                .inputFilters(new InputFilter[]{new InputFilter.AllCaps()})
                .value(vehicle.getVin())
                .actionButtonText("查询")
                .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)
                .onActionListener(this)
                .inputFilters(new InputFilter[]{new InputFilter.LengthFilter(17), new StringUtils.VINCharacterFilter()})
                .buildEditTextActionButtonConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.levelId)
                .label(getString(R.string.vehicle_model_label))
                .value(vehicle.getSpec().getLevelId())
                .hint(getString(R.string.vehicle_model_hint))
                .addExtra(KKCheDBHelper.VehicleColumns.brand, vehicle.getSpec().getBrand())
                .addExtra(KKCheDBHelper.VehicleColumns.yearGroup, vehicle.getSpec().getYearGroup())
                .addExtra(KKCheDBHelper.VehicleColumns.saleName, vehicle.getSpec().getSaleName())
                .addExtra(KKCheDBHelper.VehicleColumns.series, vehicle.getSpec().getSeries())
                .displayValue(vehicle.getSpec().getDisplayValue())
                .buildSelectorConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.quotedPriceIncludeTransferFee)
                .label(getString(R.string.quoted_price_transfer_fee_label))
                .value(StringUtils.boolToStringYesNo(vehicle.getPrice().isQuotedPriceIncludeTransferFee()))
                .buildSwitchConfig());

        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.quotedPrice)
                .label(getString(R.string.quoted_price_label))
                .caption(getString(R.string.quoted_price_caption))
                .hint(getString(R.string.quoted_price_input_hint))
                .inputFilters(new InputFilter[]{new InputFilter.LengthFilter(6)})
                .value(vehicle.getPrice().getQuotedPriceByTenGrand())
                .inputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL)
                .buildEditTextConfig());


        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.mileage)
                .label(getString(R.string.mileage_label))
                .hint(getString(R.string.mileage_input_hint))
                .caption(getString(R.string.mileage_caption))
                .inputFilters(new InputFilter[]{new InputFilter.LengthFilter(5)})
                .value(vehicle.getSummary().getMileageByTenKm())
                .inputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL)
                .buildEditTextConfig());

        List<Map<String, String>> purposes = CollectionUtils.toMapList(getResources().getStringArray(R.array.purpose_keys),
                getResources().getStringArray(R.array.purpose_labels));
        TypedArray colorIcons = getResources().obtainTypedArray(R.array.color_icons);

        List<Map<String, String>> colors = CollectionUtils.toMapList(getResources().getStringArray(R.array.color_keys),
                getResources().getStringArray(R.array.color_labels), colorIcons);

        List<Map<String, String>> interiorColors = CollectionUtils.toMapList(getResources().getStringArray(R.array.interior_color_keys),
                getResources().getStringArray(R.array.interior_color_labels));

//        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.purpose)
//                .label(getString(R.string.purpose_label))
//                .spinnerData(purposes)
//                .value(vehicle.getSummary().getPurpose())
//                .buildSpinnerConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.purpose)
                .label(getString(R.string.purpose_label))
                .radioGroupData(purposes)
                .value(TextUtils.isEmpty(vehicle.getSummary().getPurpose()) == true ? "personal" : vehicle.getSummary().getPurpose())
                .buildRadioGroupConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.color)
                .label(getString(R.string.vehicle_color_label))
                .spinnerData(colors)
                .value(vehicle.getSummary().getColor())
                .buildSpinnerConfig());

//        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.interior)
//                .label(getString(R.string.vehicle_interior_color_label))
//                .spinnerData(interiorColors)
//                .value(vehicle.getSummary().getInterior())
//                .buildSpinnerConfig());

        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.interior)
                .label(getString(R.string.vehicle_interior_color_label))
                .radioGroupData(interiorColors)
                .value(TextUtils.isEmpty(vehicle.getSummary().getInterior()) == true ? "light" : vehicle.getSummary().getInterior())
                .buildRadioGroupConfig());
//        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.color)
//                .label(getString(R.string.vehicle_color_label))
//                .spinnerData(colors)
//                .value(vehicle.getSummary().getColor())
//                .buildSpinnerConfig());

        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.registrationDate)
                .label(getString(R.string.registration_date_label))
                .value(vehicle.getVehicleDate().getRegistrationDate())
                .buildDatePickerConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.inspectionDate)
                .label(getString(R.string.inspection_date_label))
                .value(vehicle.getVehicleDate().getInspectionDate())
                .buildDatePickerConfig());
        items.add(new ItemConfig.Builder().key(KKCheDBHelper.VehicleColumns.fullDesc)
                .label(getString(R.string.full_desc_label))
                .hint(getString(R.string.full_desc_input_hint))
                .singleLine(false)
                .value(vehicle.getDesc().getDetail())
                .displayValue(vehicle.getDesc().getDetail())
                .buildSelectorConfig());
        adapter = new ItemsAdapter(items, this);
        listView.setOnItemClickListener(this);

        listView.setAdapter(adapter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void back() {
        setResult(RESULT_CANCELED);
        finish();    //finish当前activity
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_vehicle_step1,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onDestroy() {
        dbHelper.close();
        super.onDestroy();
    }
//
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                back();
                return true;
            case R.id.next:
                List<ItemConfig> items = adapter.getData();
                boolean hasError = false;
                for (ItemConfig itemConfig : items) {
                    String value = itemConfig.getValue();
                    String key = itemConfig.getKey();
                    itemConfig.setErrorMessage("");
                    if (key.equals(KKCheDBHelper.VehicleColumns.vin)) {
                        if (StringUtils.hasText(value)) {
                            if (StringUtils.isValidVIN(value)) {
                                vehicle.setVin(value);
                                itemConfig.clearErrorMessage();
                            } else {
                                hasError = hasError || true;
                                itemConfig.setErrorMessage(getString(R.string.error_msg_invalid_vin));
                            }
                        } else {
                            vehicle.setVin("");
                            itemConfig.clearErrorMessage();
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.levelId)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getSpec().setLevelId(value);
                            if (itemConfig.getExtra() != null) {
                                vehicle.getSpec().setBrand(itemConfig.getExtra().get(KKCheDBHelper.VehicleColumns.brand) + "");
                                vehicle.getSpec().setYearGroup(itemConfig.getExtra().get(KKCheDBHelper.VehicleColumns.yearGroup) + "");
                                vehicle.getSpec().setSaleName(itemConfig.getExtra().get(KKCheDBHelper.VehicleColumns.saleName) + "");
                                vehicle.getSpec().setSeries(itemConfig.getExtra().get(KKCheDBHelper.VehicleColumns.series) + "");
                            }
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_msg_vehicle_model));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.quotedPrice)) {
                        if (!StringUtils.isValidPrice(value)) {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_msg_quoted_price));
                        } else {
                            vehicle.getPrice().setQuotedPriceByTenGrand(value);
                            itemConfig.clearErrorMessage();
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.mileage)) {
                        if (!StringUtils.isValidMileage(value)) {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_msg_mileage));
                        } else {
                            vehicle.getSummary().setMileageByTenKm(value);
                            itemConfig.clearErrorMessage();
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.purpose)) {
                        vehicle.getSummary().setPurpose(value);
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.color)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getSummary().setColor(value);
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_color));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.interior)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getSummary().setInterior(value);
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_interior_color));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.registrationDate)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getVehicleDate().setRegistrationDate(value);
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_registration_date));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.inspectionDate)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getVehicleDate().setInspectionDate(value);
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_inspection_date));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.fullDesc)) {
                        if (StringUtils.hasText(value)) {
                            vehicle.getDesc().setDetail(value);
                            itemConfig.clearErrorMessage();
                        } else {
                            hasError = hasError || true;
                            itemConfig.setErrorMessage(getString(R.string.error_full_desc));
                        }
                    }
                    if (key.equals(KKCheDBHelper.VehicleColumns.quotedPriceIncludeTransferFee)) {
                        vehicle.getPrice().setQuotedPriceIncludeTransferFee(StringUtils.yesNoToBool(value));
                    }

                }
                if (hasError) {
                    adapter.refresh(items);
                    return true;
                }

                int affect = dbHelper.updateVehicle(rowId, vehicle);

                if (StringUtils.hasText(vehicle.getVin())) {
                    Intent intent = new Intent(getApplicationContext(), VehicleEditorStep3Activity.class);
                    intent.putExtra("rowId", rowId);
                    startActivityForResult(intent, STEP3);
                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                } else {
                    new AlertDialog.Builder(this)
                            .setTitle("温馨提示")
                            .setMessage("确定不填车架号么？填写车架号可直接提升车辆排名！")
                            .setPositiveButton("去填写", null)
                            .setNegativeButton("下一步", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(getApplicationContext(), VehicleEditorStep3Activity.class);
                                    intent.putExtra("rowId", rowId);
                                    startActivityForResult(intent, STEP3);
                                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                                }
                            })
                            .create().show();
                }


                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ItemConfig config = (ItemConfig) adapterView.getItemAtPosition(i);
        itemKey = config.getKey();
        if (config instanceof ItemDatePickerConfig) {
            if (datePickerFragment == null) {
                datePickerFragment = new DatePickerFragment();
                datePickerFragment.setOnDateSelectedListener(this);
            }
            if (KKCheDBHelper.VehicleColumns.registrationDate.equals(itemKey)) {
                datePickerFragment.setMinDate(0L);
                datePickerFragment.setMaxDate(DateUtils.getRegistrationDateLimitation());
            }
            if (KKCheDBHelper.VehicleColumns.inspectionDate.equals(itemKey)) {
                ItemConfig itemConfig = ((ItemsAdapter) adapterView.getAdapter()).getItemForKey(KKCheDBHelper.VehicleColumns.registrationDate);
                if (itemConfig != null && StringUtils.hasText(itemConfig.getValue())) {
                    datePickerFragment.setMinDate(DateUtils.parseToDate(itemConfig.getValue()).getTime());
                }
                //datePickerFragment.setMinDate(DateUtils.nowMillis());
                datePickerFragment.setMaxDate(DateUtils.getInspectionDateLimitation());
            }

            datePickerFragment.show(getSupportFragmentManager(), "datePicker");
            String value = config.getValue();
            if (StringUtils.hasText(value)) {
                int[] d = DateUtils.formatToArray(value);
                if (d != null) {
                    datePickerFragment.updateDate(d[0], d[1], d[2]);
                }
            }
        }
        if (config instanceof ItemSelectorConfig && KKCheDBHelper.VehicleColumns.levelId.equals(itemKey)) {
            if (StringUtils.hasText(vehicle.getId())) {
                Guard.showMessage(this, R.string.msg_cannot_modify_model_for_online_vehicle);
                return;
            }
            Intent intent = new Intent(this, VehicleModelSelectorActivity.class);
            startActivityForResult(intent, REQUEST_CODE_VEHICLE_MODEL);
            overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_top);
        }
        if (config instanceof ItemSelectorConfig && KKCheDBHelper.VehicleColumns.fullDesc.equals(itemKey)) {
            Intent intent = new Intent(this, VehicleDescriptionEditActivity.class);
            intent.putExtra("content", config.getValue());
            intent.putExtra("individualList", (Serializable) vehicle.getDesc().getIndividualList());
            intent.putExtra("highlightList", (Serializable) vehicle.getDesc().getHighlightList());
            startActivityForResult(intent, REQUEST_CODE_VEHICLE_MODEL);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_VEHICLE_MODEL) {
            if (RESULT_OK == resultCode) {
                String levelId = data.getStringExtra(KKCheDBHelper.VehicleColumns.levelId);
                String label = data.getStringExtra("label");
                String brand = data.getStringExtra(KKCheDBHelper.VehicleColumns.brand);
                String yearGroup = data.getStringExtra(KKCheDBHelper.VehicleColumns.yearGroup);
                String saleName = data.getStringExtra(KKCheDBHelper.VehicleColumns.saleName);
                String series = data.getStringExtra(KKCheDBHelper.VehicleColumns.series);
                Map<String, Object> extra = new HashMap<String, Object>();
                extra.put(KKCheDBHelper.VehicleColumns.brand, brand);
                extra.put(KKCheDBHelper.VehicleColumns.yearGroup, yearGroup);
                extra.put(KKCheDBHelper.VehicleColumns.saleName, saleName);
                extra.put(KKCheDBHelper.VehicleColumns.series, series);
                adapter.updateValueForKey(itemKey, levelId, label, extra);
            }

            if (VehicleDescriptionEditActivity.RESULT_CODE_DESCRIPTION == resultCode) {
                String content = data.getExtras().getString("newcontent");
                adapter.updateValueForKey(itemKey, content, content, new HashMap<String, Object>());
                itemKey = "";
                vehicle.getDesc().setIndividualList((List<VehicleDescIndividual>) data.getSerializableExtra("newindividual"));
                vehicle.getDesc().setHighlightList((List<VehicleDescHighlight>) data.getSerializableExtra("newhighlight"));
            }
        }
        if (requestCode == REQUEST_CODE_SCAN_VIN_BARCODE && resultCode == RESULT_OK) {
            String contents = data.getStringExtra("SCAN_RESULT");
            String format = data.getStringExtra("SCAN_RESULT_FORMAT");


            adapter.updateValueForKey(itemKey, contents);
        }

        if (requestCode == STEP3 && resultCode == RESULT_OK) {
            setResult(RESULT_OK, data);
            finish();
        }

    }

    @Override
    public void onDateSelected(DatePicker view, int year, int month, int day) {
        String date8601 = DateUtils.formatISO8601(year, ++month, day);
        adapter.updateValueForKey(itemKey, date8601);
        if (KKCheDBHelper.VehicleColumns.registrationDate.equals(itemKey)) {
            adapter.updateValueForKey(KKCheDBHelper.VehicleColumns.inspectionDate, DateUtils.calculateRegularInspectionDateByRegistration(date8601));
        }
        itemKey = "";
    }

    @Override
    public void onDateClean() {
        String date8601 = "";
        adapter.updateValueForKey(itemKey, date8601);
        if (KKCheDBHelper.VehicleColumns.registrationDate.equals(itemKey)) {
            adapter.updateValueForKey(KKCheDBHelper.VehicleColumns.inspectionDate, date8601);
        }
        itemKey = "";
    }

    @Override
    public void onDone(String content) {
        adapter.updateValueForKey(itemKey, content, content, new HashMap<String, Object>());
        itemKey = "";
    }

    @Override
    public void onAction(int who, ItemConfig itemConfig) {

        if (KKCheDBHelper.VehicleColumns.vin.equals(itemConfig.getKey())) {
            if (who == ItemEditTextWithActionButtonConfig.SUFFIX_ACTION_BUTTON) {
                String value = itemConfig.getValue();
                if (StringUtils.hasText(value) && StringUtils.isValidVIN(value)) {
                    openVinRecognition(itemConfig);
                } else {
                    Toast.makeText(this, R.string.error_msg_invalid_vin, Toast.LENGTH_LONG).show();
                }
            }
            if (who == ItemEditTextWithActionButtonConfig.PREFIX_ACTION_BUTTON) {
                itemKey = itemConfig.getKey();

//                Intent intent = new Intent(this, VinBarcodeScannerActivity.class);
//
//                startActivityForResult(intent, REQUEST_CODE_SCAN_VIN_BARCODE);
            }
        }
    }

    private void openVinRecognition(ItemConfig itemConfig) {
        VinRecognitionDialog vinRecognitionDialog = new VinRecognitionDialog();
        vinRecognitionDialog.setListener(new VinRecognitionDialog.OnChoiceMadeListener() {
            @Override
            public void onSelected(String key, Vehicle.Spec spec) {
                if (StringUtils.hasText(vehicle.getId())) {
                    Guard.showMessage(VehicleEditorStep2Activity.this, R.string.msg_cannot_modify_model_for_online_vehicle);
                    return;
                }
                String levelId = spec.getLevelId();
                String label = spec.getDisplayValue();
                String brand = spec.getBrand();
                String yearGroup = spec.getYearGroup();
                String saleName = spec.getSaleName();
                String series = spec.getSeries();
                Map<String, Object> extra = new HashMap<String, Object>();
                extra.put(KKCheDBHelper.VehicleColumns.brand, brand);
                extra.put(KKCheDBHelper.VehicleColumns.yearGroup, yearGroup);
                extra.put(KKCheDBHelper.VehicleColumns.saleName, saleName);
                extra.put(KKCheDBHelper.VehicleColumns.series, series);
                adapter.updateValueForKey(KKCheDBHelper.VehicleColumns.levelId, levelId, label, extra);
            }
        });
        vinRecognitionDialog.setKey(itemConfig.getKey());
        vinRecognitionDialog.setValue(itemConfig.getValue());
        vinRecognitionDialog.show(getSupportFragmentManager(), "vinRecognitionDialog");
    }
}
